package com.iptv.grape.bu.process;

import android.os.Handler;
import android.os.Looper;

import com.iptv.grape.bu.AppConfig;
import com.iptv.grape.bu.GrapeContext;
import com.iptv.grape.event.msg.BackVodFullScreenMsg;
import com.iptv.grape.event.msg.EpgVerReSetMsg;
import com.iptv.grape.net.Entity.TTLResult;
import com.iptv.grape.net.NetworkService;
import com.iptv.grape.util.AppConstant;
import com.iptv.grape.util.AppLog;
import com.iptv.grape.util.CommUtil;
import com.iptv.grape.util.WebViewUtils;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

// 心跳包的处理
public class TTLPlayProc {


    boolean isFirstProc = true;

    public TTLPlayProc() {

    }

    Timer timer = new Timer();

    TimerTask task = new TimerTask(){

        @Override
        public void run() {
            runHelp();
        }
    };

    private void runHelp(){
        AppLog.getCurLog().out("发送心跳包");
        try{
            if(!GrapeContext.curGrapeContext().isProcTTL()){
                GrapeContext.curGrapeContext().setProcTTL(true);
                procHelp();
                GrapeContext.curGrapeContext().setProcTTL(false);
            }
        }
        catch (Exception e){
            GrapeContext.curGrapeContext().setProcTTL(false);
            e.printStackTrace();
            AppLog.getCurLog().out(e.getMessage());
        }
        AppLog.getCurLog().out("心跳包结束");
    }

    private void reSetTimer(){
        try {
            if (timer != null) {
                timer.cancel();
                timer = null;
            }
            if (task != null) {
                task.cancel();
                task = null;
            }
            timer = new Timer();
            task = new TimerTask(){

                @Override
                public void run() {
                    runHelp();
                }
            };
            if (timer != null && task != null)
                timer.schedule(task, AppConfig.getAppConfig().getHeartBeatTime() * 1000,
                        AppConfig.getAppConfig().getHeartBeatTime() * 1000);
        }
        catch (Exception e){
            AppLog.getCurLog().out("重新设置定时器错误");
            AppLog.getCurLog().out(e.getMessage());
        }
    }

    public void init(){

        timer.schedule(task, AppConstant.FirstTTLTimeVal, AppConfig.getAppConfig().getHeartBeatTime() * 1000);
    }


    private void procHelp(){
        AppLog.getCurLog().out("执行心跳包处理--开始" + CommUtil.formatDate(new Date(),"HH:mm:ss"));
        try {
            TTLResult result = NetworkService.getInstance().sendTTLAsync();
            if(result != null) {

                if(!AppConfig.getAppConfig().getChannelVer().equals(result.getData().getChannelVer())){
                    // 同步频道信息
                    channelProcHelp(result);
                }
                else {
                    AppLog.getCurLog().out("频道版本信息相等" + AppConfig.getAppConfig().getChannelVer());
                }
                if(!AppConfig.getAppConfig().getConfigVar().equals(result.getData().getConfigVer())){
                    appConfigProcHelp(result);
                }
                else {
                    AppLog.getCurLog().out("AppConfig版本信息相等" + AppConfig.getAppConfig().getConfigVar());
                }
                if(!AppConfig.getAppConfig().getPlayBillVer().equals(result.getData().getPlayBillVer())){
                    // 同步节目单信息
                    channelBillProcHelp(result);
                }
                else {
                    AppLog.getCurLog().out("节目单版本信息相等" + AppConfig.getAppConfig().getPlayBillVer());
                }
                if(!AppConfig.getAppConfig().getAdImageVar().equals(result.getData().getAdImageVar())){
                    // 同步广告暂停背景图
                    adImageProcHelp(result);
                }
                else {
                    AppLog.getCurLog().out("广告暂停背景图信息相等" + AppConfig.getAppConfig().getAdImageVar());
                }
                if(!isFirstProc) {
                    // 如果不是第一个心跳包
                    if (!AppConfig.getAppConfig().getEpgVer().equals(result.getData().getEpgVer())) {
                        // 发送消息到H5

                        new Handler(Looper.getMainLooper()).post(new Runnable() {
                            @Override
                            public void run() {
                                //rel_horitation.reSetFocus();
                                try {
                                    WebViewUtils.sentKeyMessageToWebView(new EpgVerReSetMsg(), GrapeContext.curGrapeContext().getCurActivity().getBridgeWebView());
                                    AppLog.getCurLog().out("发送消息到H5同步缓存");
                                }
                                catch (Exception e){
                                    e.printStackTrace();
                                    AppLog.getCurLog().out(e.getMessage());
                                }

                            }
                        });


                    } else {
                        AppLog.getCurLog().out("EPG版本相等" + AppConfig.getAppConfig().getEpgVer());
                    }
                }
                else {
                    isFirstProc = false;
                }
            }
        }
        catch (Exception e){
            e.printStackTrace();
            AppLog.getCurLog().out(e.getMessage());
        }
        AppLog.getCurLog().out("执行心跳包处理--完毕" + CommUtil.formatDate(new Date(),"HH:mm:ss"));
    }

    // 同步广告暂停背景图
    private void adImageProcHelp(TTLResult result){
        try{
            AppLog.getCurLog().out("开始同步广告暂停背景图");
            boolean isSuccess = NetworkService.getInstance().getAppAdImag();
            if(isSuccess) {
                AppConfig.getAppConfig().setAdImageVar(result.getData().getAdImageVar());
                AppConfig.getAppConfig().serializeToFile();
                AppLog.getCurLog().out("同步广告暂停背景图成功");
            }
            AppLog.getCurLog().out("开始同步广告暂停背景图");
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

    // 同步节目单信息
    private void channelBillProcHelp(TTLResult result){
        try{
            AppLog.getCurLog().out("开始同步节目单信息");
            boolean isSuccess = GrapeContext.curGrapeContext().getBillList().syncData();
            if (isSuccess) {
                AppConfig.getAppConfig().setPlayBillVer(result.getData().getPlayBillVer());
                AppConfig.getAppConfig().serializeToFile();
                AppLog.getCurLog().out("同步节目单信息成功");
            } else {
                AppLog.getCurLog().out("同步节目单信息失败");
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }


    // 同步频道信息辅助方法
    private void channelProcHelp(TTLResult result){
        try {
            // 同步频道信息
            AppLog.getCurLog().out("开始同步频道信息");
            boolean isSuccess = GrapeContext.curGrapeContext().getChannelDLinkList().syncData();
            if (isSuccess) {
                AppConfig.getAppConfig().setChannelVer(result.getData().getChannelVer());
                AppConfig.getAppConfig().serializeToFile();
                AppLog.getCurLog().out("同步频道信息成功");
            } else {
                AppLog.getCurLog().out("频道信息失败");
            }
        }
        catch (Exception e){
            e.printStackTrace();
            AppLog.getCurLog().out("频道信息失败" + e.getMessage());
        }
    }

    // 同步APP配置信息辅助方法
    private void appConfigProcHelp(TTLResult result){
        try{
            boolean isSuccess = AppConfig.getAppConfig().syncData();
            if (isSuccess) {
                AppConfig.getAppConfig().setConfigVar(result.getData().getConfigVer());
                AppConfig.getAppConfig().serializeToFile();
                AppLog.getCurLog().out("APP配置同步成功");
                reSetTimer();
            } else {
                AppLog.getCurLog().out("APP配置同步失败");
            }
        }
        catch (Exception e){
            e.printStackTrace();
            AppLog.getCurLog().out("APP配置同步失败" + e.getMessage());
        }
    }
}
